#include "napi/native_api.h"
#include <hilog/log.h>
#include "add.h"
#include "dlfcn.h"
#include <js_native_api.h>

#include <js_native_api_types.h>
#include <string>
#include <node_api.h>
#include <hilog/log.h>
#include "math.h"

#define LOG_DOMAIN 0
#define LOG_TAG "demoTest"

typedef double(*Sub)(double, double);

static napi_value addSoByMakeFile(napi_env env, napi_callback_info info)
{
    OH_LOG_INFO(LOG_APP, "addSoByMakeFile");
    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);

    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);
    
    TestAdd testAdd;
    napi_value sum;
    napi_create_double(env, testAdd.add(value0 ,value1), &sum);

    return sum;
}


static napi_value addSobyDlOpenSo(napi_env env, napi_callback_info info)
{
    OH_LOG_INFO(LOG_APP, "addSobyDlOpenSo");
    napi_value args[3] = {nullptr};
    size_t argsNum = 3;
    napi_get_cb_info(env, info, &argsNum, args, nullptr, nullptr);
    
    char path[255];
    size_t size = 255;
    napi_get_value_string_utf8(env, args[0], path, 255, &size);
    double num1 = 0;
    napi_get_value_double(env, args[1], &num1);
    double num2 = 0;
    napi_get_value_double(env, args[2], &num2);
    
    void *testSub = dlopen(path, RTLD_LAZY);
    OH_LOG_INFO(LOG_APP, "load library: %{public}s", path);
    napi_value result = nullptr;
    if (testSub == nullptr) {
        OH_LOG_ERROR(LOG_APP, "cannot load library: %{public}s", dlerror());
        napi_get_undefined(env, &result);
        return result;
    }
    dlerror();
    OH_LOG_ERROR(LOG_APP, "start to debug");
    Sub sub = (Sub)dlsym(testSub, "sub");
    if (dlerror()) {
        OH_LOG_ERROR(LOG_APP, "load getSub failed: %{public}s", dlerror());
        napi_get_undefined(env, &result);
        return result;
    }
    double value = sub(num1, num2);
    dlclose(testSub);
    napi_create_double(env, value, &result);
    return result;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    napi_property_descriptor desc[] = {
        { "addSoByMakeFile", nullptr, addSoByMakeFile, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "addSobyDlOpenSo", nullptr, addSobyDlOpenSo, nullptr, nullptr, nullptr, napi_default, nullptr },
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version =1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "hello",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterHelloModule(void)
{
    napi_module_register(&demoModule);
}
